{extend name="common/base_page" /}

{block name="title"}盘点管理{/block}

{block name="jscssfile"}{/block}

{block name="css"}
<style type="text/css">
body{ background: #ffffff; height:100vh; }
.layui-fluid {
    padding-top: 5px;
}
.layui-table td{
  padding: 9px 5px;
  font-size: 13px;
}
input:disabled { background:#dddddd; }
.layui-table td input{ background:#020202; border:1px solid #dddddd; color:#ffffff; font-size:15px; font-weight:bold;}
</style>
{/block}

{block name="body"}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card-body public-tpl-row1" style="padding: 0px;">
        <form class="layui-form" action="" id="info-form" lay-filter="info-form" onsubmit="return false;">
        <fieldset class="layui-elem-field layui-field-title"><legend>盘点信息</legend></fieldset>
        <div class="layui-form-item">
            <div class="layui-inline"><label class="layui-form-label"><i class="c-red">*</i>盘点日期</label>
                <div class="layui-input-inline" style="width:313px;">
                <input type="text" name="create_time" id="create_time" autocomplete="off" class="layui-input" value="{if $info.create_time gt 0}{$info.create_time|date='Y-m-d'}{else}{:date('Y-m-d')}{/if}" readonly="true">
                </div>
            </div>
            <div class="layui-inline"><label class="layui-form-label">盘点备注</label>
                <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="remarks" autocomplete="off" class="layui-input" value="{$info.remarks}">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline" style="width:400px;">
                    <label class="layui-form-label line-top-tjbox" style="width:400px;text-align: left;">进货盈亏合计：{$info.cost_price|default='0.00'}，零售盈亏合计：{$info.ls_price|default='0.00'}</label>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left:0px;">
                  <div class="layui-inline" style="margin-right:0px;">
                    <label class="layui-form-label" style="width:30px;padding: 9px 15px 9px 0;">&nbsp;</label>
                    <div class="layui-input-inline" style="width:130px;">
                      <input type="checkbox" id="zero_noshow" name="zero_noshow" value="1" title="零库存不显示" lay-skin="primary">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label" style="width:30px;padding: 9px 15px 9px 0;">类型</label>
                    <div class="layui-input-inline">
                      <select id="drug_type" name="drug_type">
                          <option value="ALL">全部</option>
                          {volist name="ypkc_type_list" id="vo"}
                          <option value="{$vo.id}">{$vo.name}</option>
                          {/volist}
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <div class="layui-input-inline">
                      <input type="text" name="keyword" id="keyword" maxlength="10" placeholder="名称/拼音码/五笔码" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                      <a class="layui-btn layui-btn-normal" onclick="loadPageData();">搜索</a>
                      <a class="layui-btn layui-btn-normal" lay-submit="" lay-filter="info-form-excelbtn">导出</a>
                      <a class="layui-btn layui-btn-normal" lay-submit="" lay-filter="info-form-importbtn">导入</a>
                  </div>
            </div>
        </div>

            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body" style="margin-top:5px;padding:0px 0px;height: 595px;overflow-y: scroll;">
                        <table class="layui-table">
                          <colgroup>
                            <col width="200">
                            <col width="100">
                            <col width="100">
                            <col width="180">
                            <col width="115">
                            <col width="115">
                            <col width="90">
                            <col width="120">
                            <col width="150">
                            <col width="120">
                            <col width="120">
                            <col width="60">
                            <col>
                          </colgroup>
                          <thead>
                            <tr>
                              <th>名称</th>
                              <th style="text-align:center;">规格</th>
                              <th style="text-align:center;">货柜号</th>
                              <th>生产厂商</th>
                              <th style="text-align:center;">进货单价(元)</th>
                              <th style="text-align:center;">零售单价(元)</th>
                              <th style="text-align:center;">单位</th>
                              <th style="text-align:center;">账面数量</th>
                              <th style="text-align:center;">实际数量</th>
                              <th style="text-align:center;">进货盈亏(元)</th>
                              <th style="text-align:center;">零售盈亏(元)</th>
                              <th style="text-align:center;">操作</th>
                            </tr>
                          </thead>
                          <tbody id="subListBox">
                          </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-layout-admin">
              <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                  <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="info-form-savebtn1">保存</button>
                  <button type="reset" class="layui-btn layui-btn-danger" id="resetBtn">重置</button>
                  <a class="layui-btn layui-btn-primary" href="{:url('index/store.pandian/index')}">返回</a>
                </div>
              </div>
            </div>
            <input type="hidden" name="id" value="{$info.id}"/>
        </form>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script src="__STATIC__/ajaxfileupload.js?v={$site.version}"></script>
<script src="__INDEX__/js/store/pandian.js?v={$site.version}"></script>
<script type="text/javascript">
var _allPrice = parseFloat('0'); //parseFloat("{$info.ls_price|default='0.00'}"); //零售总金额
var _allCostPrice = parseFloat('0'); //parseFloat("{$info.cost_price|default='0.00'}"); //进货总金额
$(function(){
  setTimeout(function(){
    //日期范围
    laydate.render({ elem: '#create_time' });
    //保存数据
    layform.on('submit(info-form-savebtn1)', function(data){
      data.field.id = '{$info.id}';
      data.field.all_price = _allPrice;
      data.field.all_cost_price = _allCostPrice;
      data.field.table_list = getListData(); //列表信息
      if(Object.keys(data.field.table_list).length <= 0)
      {
          layer.msg('请先添加盘点明细后再保存', {time: 2000,anim: 4});
          return false;
      }
      layer.confirm('确定要保存盘点信息吗？', {
          title:'保存',shade:[0.4,'#000000'],btn: ['确定','取消'],
          btn1:function(){
              layer.closeAll();
              _Index.ajax("{:url('index/store.pandian/save_info')}",data.field,function(d){
                if(d.err == 0)
                {
                  $('#subListBox').empty();
                  reloadListPrice();
                  $('#resetBtn').trigger('click');
                  layer.msg(d.msg, {time: 1000},function(){ 
                    window.location.href = "{:url('index/store.pandian/index')}";
                  });
                }else{
                  layer.msg(d.msg, {time: 2000,anim: 4});
                }
              },'请稍候...');
          }
      });
      return false;
    });
    //导出
    layform.on('submit(info-form-excelbtn)', function(data){
        data.field.id = '{$info.id}';
        data.field.all_price = _allPrice;
        data.field.all_cost_price = _allCostPrice;
        data.field.table_list = getListData(); //列表信息
        if(Object.keys(data.field.table_list).length <= 0)
        {
            layer.msg('请先添加盘点明细后再导出', {time: 2000,anim: 4});
            return false;
        }
        layer.confirm('导出需要一些时间，确定要导出吗？<br/><span style="color:#ff0000">注意：导出文件将作为导入的模板文件。</span>', {
            title:'导出Excel',shade:[0.4,'#000000'],btn: ['确定','取消'],
            btn1:function(){
                layer.closeAll();
                __showLoadingBox('正在导出...');
                var _url = "{:url('index/store.pandian/save_excel')}";
                var _iframe = '<form action="'+_url+'" id="saveExcelForm" method="post">\
                              <input type="hidden" name="pdata" value=\''+JSON.stringify(data.field)+'\'/>\
                            </form>';
                if($("#saveExcelForm").length > 0) $("#saveExcelForm").remove();
                $('body').append(_iframe);
                $("#saveExcelForm").submit();
                setTimeout(function(){ __hideLoadingBox(); }, 1500);
            }
        });
    });
    //导入
    layform.on('submit(info-form-importbtn)', function(data){
        var _tag = 'pandian', _imgHtml = '<input type="file" accept="application/vnd.ms-excel,xls" name="img_file" id="img_more_file" class="ipt-file-hide">';
        if($('#img_more_file').length) $('#img_more_file').remove();
        $('body').append(_imgHtml);
        $('#img_more_file').on('change',function(b){
            if($(this).val() != '')
            {
                AjaxUploadFile({
                    tag:_tag,
                    savedir:'excel_' + _tag,
                    inputname:$(this).attr('name'),
                    imgid:$(this).attr('id')
                });
            }
        });
        $('#img_more_file').trigger('click');
    });
    layform.render();
  },50);
  loadPageData();
});

//加载数据
function loadPageData()
{
  var _zero_noshow = 0;
  if($('#zero_noshow').prop('checked')) _zero_noshow = 1;
  _Index.ajax("{:url('index/store.pandian/info')}",{
    id:'{$info.id}',
    drugtype:$.trim($('#drug_type').val()),
    zeronoshow:$.trim(_zero_noshow),
    keyword:$.trim($('#keyword').val())
  },function(d){
      if(d.err == 0)
      {
        __showLoadingBox('请稍候...');
        //设置-列表
        setTableTbodyHtml(d);
        __hideLoadingBox();
      }else{
        layer.msg(d.msg, {time: 2000,anim: 4});
      }
  },'请稍候...');
}
//上传动作
function AjaxUploadFile(param)
{
    __showLoadingBox('上传中，请稍候...');
    $.ajaxFileUpload({
        url: "{:url('index/index/upload_excel')}", //用于文件上传的服务器端请求地址
        secureuri: false, //是否需要安全协议，一般设置为false
        fileElementId: param.imgid, //文件上传域的ID
        data:param, //要上传参数
        dataType:"json", //返回值类型 一般设置为json
        success: function(data)  //服务器成功响应处理函数
        {
            if($('#img_more_file').length) $('#img_more_file').remove();
            __hideLoadingBox();
            if(data.err == 0)
            {
                importAction("{:url('index/store.pandian/import_excel')}",{id:'{$info.id}',path:encodeURIComponent(data.path)});
            }else{
                layer.msg(data.msg, {time: 2000,anim: 6});
            }
        },
        error: function (data, status, e)//服务器响应失败处理函数
        {
            __hideLoadingBox();
            layer.msg('上传出错：' + e, {time: 2000,anim: 6,shade:[0.4,'#000000']});
            return false;
        }
    });
}
//执行导入
function importAction(_url,_param)
{
    _Index.ajax(_url,_param,function(d){
        if(d.err == 0)
        {
            __showLoadingBox('请稍候...');
            //设置-列表
            setTableTbodyHtml(d);
            //重新计算价格
            reloadListPrice();
            __hideLoadingBox();
        }else{
            layer.msg(d.msg, {time: 2000,anim: 4});
        }
    },'导入中，请稍候...');
}
</script>
{/block}